package com.webull.information.center.carwler.task.handler;

import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;
import com.webull.information.center.carwler.common.util.news.ReutersNewsListUitl;
import com.webull.information.center.carwler.common.util.news.ReutersNewsListUitl.ReutersItem;
import com.webull.information.center.carwler.task.support.TikcerNewsProcessor;
import com.webull.information.center.common.constants.Constants;
import com.webull.information.center.common.constants.UrlConstants;
import com.webull.information.client.model.News;
import com.webull.information.client.model.parm.NewsRelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新浪新闻处理
 *
 * @author shimingjun
 * @version 1.0
 * @date 2016年8月26日 下午4:06:09
 * @since JDK 1.8
 */
@Component
public class StockReuterEnNewsHandler {

    protected final Logger logger = LogManager.getLogger(getClass());
    private static String reuter_lang = Constants.lang_en;// 新浪语言版本
    private @Autowired
    ReutersNewsListUitl reutersNewsListUitl;
    private @Autowired
    TikcerNewsProcessor tikcerNewsProcessor;

    public void handlerByStockTitle4Reuters(StockTitle stockTitle) {
        String symbol = stockTitle.getSymbol();
        String newsProfile = getReuterProfileUrl(stockTitle.getExchangeCode(), symbol);
        processReutersList(stockTitle, newsProfile);
    }

    private void processReutersList(StockTitle stockTitle, String newsProfile) {
        try {

            /**
             * 获取标的物新闻首页的列表
             */
            List<ReutersItem> newsList = reutersNewsListUitl.parseNewsList(newsProfile);
            //
            entrie:
            for (int i = 0; newsList != null && i < newsList.size(); i++) {
                try {
                    ReutersItem item = newsList.get(i);
                    processReutersNews(stockTitle, item);
                } catch (Exception e) {
                    logger.warn(e);
                    break entrie;
                }
            }
        } catch (Exception e) {
            logger.error("p2rocessReutersList ", e);
        }
    }

    private void processReutersNews(StockTitle stockTitle, ReutersItem item) throws Exception {
        String title = item.getTitle();
        String link = item.getLink();
        NewsInformation info = new NewsInformation();// produce(doc);

        info.setTitle(title);
        info.setRegionId(stockTitle.getRegionId());

        info.setSummary(item.getSummary());
        info.setUrl(link);
        info.setLanguage(reuter_lang);
        info.setCollectSource(News.CollectSource.reutersEn);
        String md5 = HttpJsoupClient.finishingMd5Url(link);
        info.setUrlMd5(md5);

        tikcerNewsProcessor.bindTargetNewsRecord(info, stockTitle.getTickerId(), NewsRelType.ticker);
    }

    /**
     * 转换成新浪个股资讯首页
     *
     * @param exchange
     * @param symbol
     * @return
     */
    private String getReuterProfileUrl(String exchange, String symbol) {
        Map<String, String> map = new HashMap<>();
        map.put("SHH", ".SS");// 上海
        map.put("SHZ", ".SZ");// 深圳
        map.put("HKG", ".HK");// 深圳
        symbol = "HKG".equalsIgnoreCase(exchange) ? StringUtils.substring(StringUtils.leftPad(symbol, 4, "0"), -4)
                : symbol;
        return StringUtils.replaceOnce(UrlConstants.reutersNewsUnitUrl, "{symbol}",
                symbol + map.getOrDefault(exchange, ".O"));
    }

}
